home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 19 / macformat_19.iso / Shareware / Games / Star Flick / CT / Source / GammaFade.c < prev    next >
Text File  |  1994-02-06  |  6KB  |  206 lines

  1. /******************************************************************************
  2.  GammaFade.c
  3.                             
  4.  ******************************************************************************/
  5.  
  6. #include <QDOffscreen.h>
  7. #include <QuickDraw.h>
  8. #include <Video.h>
  9. #include <Traps.h>
  10.  
  11. GammaTblPtr      newGammaTable;
  12. GammaTblPtr      savedGammaTable;
  13. GDHandle         mainDevice;
  14. CTabHandle       mainDeviceColors;
  15. VDGammaRecord    newGamma;
  16. VDSetEntryRecord setEntriesRec;
  17. unsigned char    *newTable;
  18. unsigned char    *savedTable;
  19. VDGammaRecord    currentGamma;
  20. GammaTblPtr      currentGammaTable;
  21. Boolean             gammaFadedOut;
  22.  
  23. void GammaFadeOut(void);
  24. void GammaFadeIn(void);
  25. pascal short MyVideoStatus(short refNum, short code, Ptr param);
  26.  
  27. Boolean IsGammaFaded()
  28. {
  29.     return gammaFadedOut;
  30. }
  31.  
  32. void GammaFadeOut(void)
  33. {
  34.     QDErr            error;
  35.     short           count;
  36.     long            fadeAmount;
  37.     short           channelIndex;
  38.     Ptr                csPtr;
  39.     long            tableSize;
  40.     
  41.     if (IsGammaFaded())
  42.         return;
  43.  
  44.     mainDevice = GetMainDevice ();
  45.  
  46.     /* Grab the main device’s gamma table; VideoStatus in Sample.a */
  47.     error = MyVideoStatus( (**mainDevice).gdRefNum, cscGetGamma,
  48.             (Ptr)¤tGamma );
  49.  
  50.     /* Grab a pointer to the main device’s gamma table */
  51.     currentGammaTable = (GammaTblPtr)currentGamma.csGTable;
  52.  
  53.     // JAB 12/23/93
  54.     if (currentGammaTable->gDataWidth > 16)
  55.         return;
  56.     tableSize = (long) currentGammaTable->gDataCnt * (currentGammaTable->gDataWidth <= 8? 1 : 2);
  57.  
  58.     /* Alloc mem for new gamma table */
  59.     savedGammaTable = (GammaTblPtr)NewPtr( sizeof (GammaTbl) +
  60.             tableSize * currentGammaTable->gChanCnt +
  61.             currentGammaTable->gFormulaSize );
  62.  
  63.     /* Copy main device’s gamma into new gamma except for table itself */
  64.     BlockMove( (Ptr)currentGammaTable, (Ptr)savedGammaTable,
  65.             sizeof (GammaTbl) + tableSize * currentGammaTable->gChanCnt +
  66.             currentGammaTable->gFormulaSize );
  67.  
  68.     /* Alloc mem for new gamma table */
  69.     newGammaTable = (GammaTblPtr)NewPtr( sizeof (GammaTbl) +
  70.             tableSize * currentGammaTable->gChanCnt +
  71.             currentGammaTable->gFormulaSize );
  72.  
  73.     /* Copy main device’s gamma into new gamma except for table itself */
  74.     BlockMove( (Ptr)savedGammaTable, (Ptr)newGammaTable,
  75.             sizeof (GammaTbl) );
  76.  
  77.     /* Fade out the screen */
  78.     for (fadeAmount = 100; fadeAmount >= 0; fadeAmount -= 4)
  79.     {
  80.         /* Get pointers to the actual tables */
  81.         newTable = (unsigned char *)&newGammaTable->gFormulaData +
  82.                 newGammaTable->gFormulaSize;
  83.         savedTable = (unsigned char *)&savedGammaTable->gFormulaData +
  84.                 savedGammaTable->gFormulaSize;
  85.  
  86.         /* For each channel in the table, fade it */
  87.         for (channelIndex = 0; channelIndex < savedGammaTable->gChanCnt; ++channelIndex)
  88.         {
  89.             /* For each entry in each table channel, fade it */
  90.             if (savedGammaTable->gDataWidth <= 8) {
  91.                 for (count = 0; count < savedGammaTable->gDataCnt; count++)
  92.                     newTable[count] = savedTable[count] * fadeAmount / 100L;
  93.             }
  94.             else {
  95.                 unsigned short *nt,*st;
  96.                 nt = (unsigned short *) &newTable[0];
  97.                 st = (unsigned short *) &savedTable[0];
  98.                 for (count = 0; count < savedGammaTable->gDataCnt; count++)
  99.                     nt[count] = st[count] * fadeAmount / 100L;
  100.             }
  101.             /* Point to the next channels */
  102.             newTable += newGammaTable->gDataCnt;
  103.             savedTable += savedGammaTable->gDataCnt;
  104.         }
  105.  
  106.         /* Write the new gamma table to the main device */
  107.         newGamma.csGTable = (Ptr)newGammaTable;
  108.         csPtr = (Ptr) &newGamma;
  109.         error = Control( (**mainDevice).gdRefNum, cscSetGamma, (Ptr) &csPtr );
  110.  
  111.         /* Force the main device to use the new gamma table */
  112.         mainDeviceColors = (**(**mainDevice).gdPMap).pmTable;
  113.         setEntriesRec.csTable = (ColorSpec *)&(**mainDeviceColors).ctTable;
  114.         setEntriesRec.csStart = 0;
  115.         setEntriesRec.csCount = (**mainDeviceColors).ctSize;
  116.         csPtr = (Ptr) &setEntriesRec;
  117.         error = Control ((**mainDevice).gdRefNum, cscSetEntries, (Ptr) &csPtr);
  118.     }
  119.     gammaFadedOut = true;
  120. }
  121.  
  122. void GammaFadeIn(void)
  123. {
  124.     long             fadeAmount;
  125.     unsigned char    *newTable;
  126.     unsigned char    *savedTable;
  127.     short            count;
  128.     short            channelIndex;
  129.     VDGammaRecord    newGamma;
  130.     Ptr                 csPtr;
  131.     QDErr            error;
  132.     VDSetEntryRecord setEntriesRec;
  133.  
  134.     // SysBeep(1);
  135.  
  136.     if (!IsGammaFaded())
  137.         return;
  138.  
  139.     /* Fade in the screen */
  140.     for (fadeAmount = 0; fadeAmount <= 100; fadeAmount += 4)
  141.     {
  142.         /* Get pointers to the actual tables */
  143.         newTable = (unsigned char *)&newGammaTable->gFormulaData +
  144.                 newGammaTable->gFormulaSize;
  145.         savedTable = (unsigned char *)&savedGammaTable->gFormulaData +
  146.                 savedGammaTable->gFormulaSize;
  147.  
  148.         /* For each channel in the table, fade it */
  149.         for (channelIndex = 0; channelIndex < savedGammaTable->gChanCnt; ++channelIndex)
  150.         {
  151.             /* For each entry in each table channel, fade it */
  152.             if (savedGammaTable->gDataWidth <= 8) {
  153.                 for (count = 0; count < savedGammaTable->gDataCnt; count++)
  154.                     newTable[count] = savedTable[count] * fadeAmount / 100L;
  155.             }
  156.             else {
  157.                 unsigned short *nt,*st;
  158.                 nt = (unsigned short *) &newTable[0];
  159.                 st = (unsigned short *) &savedTable[0];
  160.                 for (count = 0; count < savedGammaTable->gDataCnt; count++)
  161.                     nt[count] = st[count] * fadeAmount / 100L;
  162.             }
  163.  
  164.             /* Point to the next channels */
  165.             newTable += newGammaTable->gDataCnt;
  166.             savedTable += savedGammaTable->gDataCnt;
  167.         }
  168.  
  169.         /* Write the new gamma table to the main device */
  170.         newGamma.csGTable = (Ptr) newGammaTable;
  171.         csPtr = (Ptr) &newGamma;
  172.         error = Control ((**mainDevice).gdRefNum, cscSetGamma, (Ptr) &csPtr);
  173.  
  174.         /* Force the main device to use the new gamma table */
  175.         mainDeviceColors = (**(**mainDevice).gdPMap).pmTable;
  176.         setEntriesRec.csTable = (ColorSpec *)&(**mainDeviceColors).ctTable;
  177.         setEntriesRec.csStart = 0;
  178.         setEntriesRec.csCount = (**mainDeviceColors).ctSize;
  179.         csPtr = (Ptr) &setEntriesRec;
  180.         error = Control ((**mainDevice).gdRefNum, cscSetEntries, (Ptr) &csPtr);
  181.     }
  182.  
  183.     DisposPtr( (Ptr)newGammaTable );
  184.     DisposPtr( (Ptr)savedGammaTable );
  185.  
  186.     gammaFadedOut = 0;
  187. }
  188.  
  189. pascal short MyVideoStatus (short refNum, short code, Ptr param)
  190. {
  191.     struct    CntrlParam    sBlock;
  192.     short    result;
  193.     sBlock.ioCRefNum = refNum;
  194.     sBlock.csCode = code;
  195.     *((long *) &sBlock.csParam[0]) = (long) param;
  196.  
  197.     asm {
  198.         lea        sBlock, a0
  199.         dc.w    0xA005
  200.         move.l    (sp)+,a0
  201.         move.w    d0, result                
  202.     }
  203.     return result;
  204. }
  205.  
  206.